import { QuerySubMenus } from '@/services/School/SchoolMenuApi';
import { ServerMessages } from '@/services/ServerMessages';
import {MergeMenu, ShowError} from '@/utils/format';
import { PageContainer } from '@ant-design/pro-components';
import { Button, message, Space, Table } from 'antd';
import { Component } from 'react';
import { Link } from 'umi';

class ResearchPlatform extends Component {
  state = {
    loading: true,
    data: [],
  };

  componentDidMount() {
    // console.log('@请求')
    // const userInfo = getUserInfo()
    // console.log(userInfo.token)
    // request('http://192.168.31.34:3007/api/index/disci-cons/progress/', {
    //   method: 'POST',
    //   headers: {
    //     'Content-Type': 'application/json;charset=utf-8',
    //     'Authorization': userInfo.token
    //   },
    //   data: {"id":["1-1-1", "1-1-2","1-1-3","1-1-4"]},
    //   timeout: 3000,
    // }).then(response => {
    //   console.log('@response', response)
    //   let data = [
    //     {
    //       key: '1-1-1',
    //       name: '学科建设进展情况写实',
    //       circle: '每季度一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/progress-situation',
    //     },
    //     {
    //       key: '1-1-2',
    //       name: '学科评估情况',
    //       circle: '五年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/disci-eval-situation',
    //     },
    //     {
    //       key: '1-1-3',
    //       name: '学科影响力情况',
    //       circle: '一年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '/disci-cons/progress/disci-influence',
    //     },
    //     {
    //       key: '1-1-4',
    //       name: '本学科建设经费数额',
    //       circle: '一年一次',
    //       target: '学科填报',
    //       flag: 0,
    //       to: '',
    //     },
    //   ]
    //   // 将 response 的 flag 映射到 data 的 flag 上
    //   for (let i = 0; i < data.length; i++) {
    //     for (let j = 0; j < response.length; j++) {
    //       console.log(data[i].key, response[j].id)
    //       if (data[i].key === response[j].id) {
    //         data[i].flag = response[j].flag
    //         break
    //       }
    //     }
    //   }
    //   this.setState({loading: false, data})
    //   console.log(data)
    // }, () => {
    //   this.setState({loading: false})
    //   ShowError('服务器故障')
    // })

    let data = [
      {
        key: '4_2_1_0',
        name: '科研创新平台建设存量及清单',
        circle: '一次性',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/platform-platform-counts',
      },
      {
        key: '4_2_1_1',
        name: '国家级平台清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/platform-tech-counts',
      },
      // {
      //   key: '4_2_1_2',
      //   name: '其他国家级平台清单',
      //   circle: '一年一次',
      //   target: '学科填报',
      //   flag: 1,
      //   to: '/research/research-platform/platform-nation-counts',
      // },
      {
        key: '4_2_1_3',
        name: '省部级平台清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/platform-province-counts',
      },
      {
        key: '4_2_2_0',
        name: '主持科研项目存量及清单',
        circle: '一次性',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/list-project-counts',
      },
      {
        key: '4_2_2_1',
        name: '主持国家重点重大项目清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/list-top-counts',
      },
      {
        key: '4_2_2_2',
        name: '主持其他国家项目清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/list-nation-counts',
      },
      {
        key: '4_2_2_3',
        name: '主持省级重点重大项目清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/list-province-counts',
      },
      {
        key: '4_2_3_1',
        name: '纵向到校科研经费数',
        circle: '每季度一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/funds-portrait-counts',
      },
      {
        key: '4_2_3_2',
        name: '横向到校科研经费数',
        circle: '每季度一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/funds-transverse-counts',
      },
      {
        key: '4_2_4',
        name: '主办的学术期刊清单',
        circle: '一年一次',
        target: '学科填报',
        flag: 1,
        to: '/research/research-platform/research-international-influence',
      },
    ];
    // 将 response 的 flag 映射到 data 的 flag 上
    // this.setState({loading: false, data})

    QuerySubMenus('4_2').then(
      (response) => {
        console.log('@返回数据', response);
        data = MergeMenu(data, response);
        console.log(data);
        this.setState({ loading: false, data });
      },
      () => {
        ShowError(ServerMessages.error_message);
      },
    );
  }

  render() {
    const columns = [
      {
        title: '监测点',
        dataIndex: 'name',
        key: 'name',
        render: (text) => <b>{text}</b>,
      },
      {
        title: '填报周期',
        dataIndex: 'circle',
        key: 'circle',
        render: (text) => <b>{text}</b>,
      },
      {
        title: '填报对象',
        dataIndex: 'target',
        key: 'target',
      },
      {
        title: '操作',
        key: 'action',
        render: (_, record) => (
          <Space size="middle">
            {record.is_period === 0 && (
              <Button type="primary" disabled>
                未到填报时间
              </Button>
            )}
            {record.is_period === 1 && record.is_filled === 1 && (
              <>
                <Button type="primary" disabled>
                  已填报
                </Button>
                <Link to={record.to + '?flag=display'}>
                  <Button type="default">查看</Button>
                </Link>
              </>
            )}
            {record.is_period === 1 && record.is_filled === 0 && (
              <Link to={record.to}>
                <Button type="primary">填报</Button>
              </Link>
            )}
          </Space>
        ),
      },
    ];

    return (
      <div>
        <PageContainer
          header={{
            title: '请选择填报项目',
            breadcrumb: {},
          }}
        >
          <Table
            loading={this.state.loading}
            columns={columns}
            dataSource={this.state.data}
            pagination={{ pageSize: 100 }}
          />
        </PageContainer>
      </div>
    );
  }
}

export default ResearchPlatform;
